/*
解析配置文件
*/
package config

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

type Conf struct {
	BrokerList []string `json:"brokerList"`
	MaxTaskNum int      `json:"maxTaskNum"`
	Partition  int      `json:"partition"`
}

func GetConfig() *Conf {
	rawConf, err := ioutil.ReadFile("./conf/conf.json")
	if err != nil {
		panic("can not read conf file " + err.Error())
	}
	conf := Conf{}
	if err := json.Unmarshal(rawConf, &conf); err != nil {
		panic("parse conf.json fail " + err.Error())
	}
	bls := os.Getenv("broker_list")
	if bls != "" {
		bl := strings.Split(bls, ",")
		conf.BrokerList = bl
	}
	mtn := os.Getenv("max_task_num")
	if mtn != "" {
		conf.MaxTaskNum, err = strconv.Atoi(mtn)
		if err != nil {
			panic("parse max_task_num from env fail")
		}
	}
	ptd := os.Getenv("partition")
	if ptd != "" {
		conf.Partition, err = strconv.Atoi(ptd)
		if err != nil {
			panic("parse partition fail " + err.Error())
		}
	}
	return &conf
}
